package com.sdtbu.ylgh.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.sdtbu.ylgh.dao.GhRosterDao;
import com.sdtbu.ylgh.entity.GhRosterEntity;
import com.sdtbu.ylgh.utils.JwtHelper;
import com.sdtbu.ylgh.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.sdtbu.ylgh.entity.GhOrderEntity;
import com.sdtbu.ylgh.service.GhOrderService;



/**
 * 挂号订单表
 *
 * @author mzd
 * @email 1695165447@qq.com
 * @date 2021-03-13 17:53:04
 */
@CrossOrigin
@RestController
@RequestMapping("ylgh/ghorder")
public class GhOrderController {
    @Autowired
    private GhOrderService ghOrderService;

    @Autowired
    private GhRosterDao ghRosterDao;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestHeader(value="token", defaultValue = "") String token, @RequestParam Map<String, Object> params){
        Long userID = JwtHelper.getUserId(token);
        if (userID == null){
            return R.error("未登录！");
        }
        params.put("user_id", userID);
        List<Map<String, Object>> list = ghOrderService.listToNew(params);

        return R.ok().put("order", list);
    }




    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestHeader(value="token", defaultValue = "") String token, @RequestBody GhOrderEntity ghOrder){
        Long userID = JwtHelper.getUserId(token);
        if (userID == null){
            return R.error("未登录！");
        }

        ghOrder.setUserId(userID);
        System.out.println("挂号订单信息：" + ghOrder);
        boolean b = ghOrderService.saveInfo(ghOrder);
        if (b)
            return R.ok();
        else
            return R.error("挂号失败！");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody GhOrderEntity ghOrder){
		ghOrderService.updateById(ghOrder);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("ylgh:ghorder:delete")
    public R delete(@RequestBody Long[] ids){
		ghOrderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 取消
     * @return
     */
    @RequestMapping("/cancel")
    public R cancel(@RequestBody Map<String, Object> params){
        System.out.println(params);
        Long rosterid = new Long(String.valueOf(params.get("roster_id")));
        GhRosterEntity ghRosterEntity = ghRosterDao.selectById(rosterid);

        GhRosterEntity ghRosterEntityNew = new GhRosterEntity();
        ghRosterEntityNew.setId(ghRosterEntity.getId());
        ghRosterEntityNew.setReserveNum(ghRosterEntity.getReserveNum()-1);
        ghRosterDao.updateById(ghRosterEntityNew);


        GhOrderEntity ghOrder = new GhOrderEntity();
        ghOrder.setId(new Long(String.valueOf(params.get("id"))));
        ghOrder.setOrderStatus((Integer) params.get("orderStatus"));
        ghOrderService.updateById(ghOrder);

        return R.ok();
    }

}
